<template>
  <div class="articles">
    <!-- 上部表单 -->
    <el-card class="box-card">
      <template v-slot:header>
        <my-breadcrumb>内容管理</my-breadcrumb>
      </template>

      <el-form ref="form" :model="form" label-width="50px">
        <el-form-item label="状态">
          <el-radio-group v-model="form.status">
            <el-radio :label="null">全部</el-radio>
            <el-radio :label="0">草稿</el-radio>
            <el-radio :label="1">待审核</el-radio>
            <el-radio :label="2">审核通过</el-radio>
            <el-radio :label="3">审核失败</el-radio>
            <el-radio :label="4">已删除</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="频道">
          <el-select
            v-model="form.channel_id"
            placeholder="请选择频道"
            clearable
          >
            <el-option
              v-for="item in channels"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="日期">
          <el-date-picker
            v-model="form.date"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            format="yyyy-MM-dd"
            value-format="yyyy-MM-dd"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="search">查询</el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <!-- 下部表格 -->
    <el-card>
      <template v-slot:header>
        <div>
          根据筛选条件共查询到{{ total_count }}条数据,当前是{{ current_page }}页
        </div>
      </template>

      <!--
        el-table 整个的表格组件
        1.:data 配置表格渲染的数据
        2.style='width: 100%' 设置表格宽
      el-table-column  表格的一列
        1. label 配置列的名字
        2. prop配置列的数据 需要执行成遍历得到的对象的属性
      -->
      <el-table
        :data="articles"
        style="width: 100%"
        v-loading="loading"
        element-loading-text="拼命加载中"
        element-loading-spinner="el-icon-loading"
        element-loading-background="rgba(0, 0, 0, 0.8)"
      >
        <el-table-column label="封面">
          <template v-slot:default="obj">
            <el-image
              style="width: 150px; height: 100px"
              fit="cover"
              :src="obj.row.cover.images[0]"
            >
              <div slot="error" class="image-slot">
                <img
                  src="@/assets/error.gif"
                  alt=""
                  style="width: 150px; height: 100px"
                />
              </div>
            </el-image>
          </template>
        </el-table-column>
        <el-table-column prop="title" label="标题"></el-table-column>
        <el-table-column prop="status" label="状态">
          <template v-slot:default="obj">
            <el-tag type="warning" v-if="obj.row.status === 0">草稿</el-tag>
            <el-tag type="info" v-if="obj.row.status === 1">待审核</el-tag>
            <el-tag type="success" v-if="obj.row.status === 2">审核通过</el-tag>
            <el-tag v-if="obj.row.status === 3">审核失败</el-tag>
            <el-tag type="danger" v-if="obj.row.status === 4">已删除</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="pubdate" label="发布时间"> </el-table-column>
        <el-table-column label="操作">
          <template v-slot:default="obj">
            <!-- {{ obj.row.id }} -->
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="small"
              circle
              @click='edit(obj.row.id)'
            ></el-button>
            <el-button
              @click="del(obj.row.id)"
              type="danger"
              icon="el-icon-delete"
              size="small"
              circle
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        background
        layout="prev, pager, next"
        :total="total_count"
        :page-size="10"
        :current-page="current_page"
        @current-change="handleCurrentChange">
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
import { reqGetArticles, reqGetChannels, reqDeleteArticles } from '@/api/articles'
// import MyBreadcrumb from '@/components/my-breadcrumb.vue'
export default {
  name: 'articles',
  data () {
    return {
      form: {
        status: null,
        date: [],
        channel_id: null
      },
      articles: [],
      current_page: 1,
      total_count: 0,
      channels: [],
      loading: false,
      searchObj: {
        status: null,
        date: [],
        channel_id: null
      }
    }
  },
  // 局部注册组件
  // components: {
  //   MyBreadcrumb
  // },
  created () {
    this.getArticles()
    this.getChannels()
  },
  methods: {
    // 根据条件获取对应文章
    getArticles () {
      const [beginPubdate, endPubdate] = this.searchObj.date || []
      // console.log(this.form.date)
      this.loading = true
      reqGetArticles({
        page: this.current_page, // 页数
        begin_pubdate: beginPubdate, // 开始时间
        end_pubdate: endPubdate, // 结束时间
        status: this.searchObj.status, // 文章状态
        channel_id: this.searchObj.channel_id || null // 没有参数就给给空
      }).then((res) => {
        // console.log(res)
        this.articles = res.data.data.results
        this.total_count = res.data.data.total_count
        // console.log(this.articles)
        this.loading = false
      })
    },
    // 获取频道信息
    getChannels () {
      reqGetChannels().then((res) => {
        // console.log(res.data)
        this.channels = res.data.data.channels
        // console.log(this.channels)
      })
    },
    del (id) {
      // 给后台传输的数据应该是articleId，要转成字符串
      const articleId = id.toString()
      this.$confirm('是否确认删除?', '温馨提示', {
        type: 'warning'
      }).then(() => {
        reqDeleteArticles(articleId).then(res => {
          this.$message.success('删除成功')
        }).catch(err => {
          this.$message.error('目前测试阶段无法删除')
          return err
        })
      }).catch(() => {
        console.log('已删除')
      })
    },
    handleCurrentChange (index) {
      // console.log(index)
      this.current_page = index
      this.getArticles()
    },
    search () {
      this.searchObj.status = this.form.status
      this.searchObj.date = this.form.date
      this.searchObj.channel_id = this.form.channel_id
      // 指定当前页为第一页
      this.current_page = 1
      // 重新获取数据
      this.getArticles()
    },
    edit (id) {
      const articleId = id.toString()
      this.$router.push(`/edit-articles/${articleId}`)
    }
  }
}
</script>

<style lang='less'>
.box-card {
  margin-bottom: 20px;
}
.el-pagination {
  margin-top: 20px;
}
</style>
